﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MPev
{
    public enum MPClassType {MPNoun, MPVerb, Noun};

    public class MPClass
    {
        static int count = 0; // gives a distinct 'id' to each instance of MPClass

        public string name;
        public int id;
        public MPClassType type;
        public List<string> attr;

        public MPClass()
        {
            this.type = MPClassType.Noun;
            id = count++;
            name = "";
            attr = new List<string>();
        }
        
        /*
        public MPClass(MPClassType type)
        {
            this.type = type;
            id = count++;
            name = "";
            attr = new List<string>();
        }

        public MPClass(MPClassType type, string name)
        {
            this.type = type;
            id = count++;
            this.name = name;
            attr = new List<string>();
        }

        public MPClass(MPClassType type, string name, string attr1, string attr2, string attr3)
        {
            this.type = type;
            id = count++;
            this.name = name;
            attr = new List<string>();
            if (attr1 != "") attr.Add(attr1);
            if (attr2 != "") attr.Add(attr2);
            if (attr3 != "") attr.Add(attr3);
        }
        */

        public string toString4Dot()
        {
            StringWriter sw = new StringWriter();

            sw.Write(id);
            sw.Write("[label = \"{");
            sw.Write(name);

            switch (type)
            {
                case MPClassType.MPNoun:
                    sw.Write("\\n\\<\\<MP\\>\\>");
                    break;
                case MPClassType.MPVerb:
                    sw.Write("\\n\\<\\<Verb\\>\\>");
                    break;case MPClassType.Noun:
                    break;
            }

            // if attribute exists
            if (attr.Count() > 0)
            {
                sw.Write("|");
                foreach (string a in attr)
                {
                    sw.Write(a);
                    //sw.Write("\\l"); // left align
                    sw.Write("\\n"); // center align
                }

            }
            sw.WriteLine("}\"]");

            return sw.ToString();
        }

        public void Load(StreamReader sr)
        {
            attr.Clear();

            name = sr.ReadLine();
            id = Convert.ToInt32(sr.ReadLine());
            //type = (MPClassType)Convert.ToInt32(sr.ReadLine());
            type = (MPClassType)Enum.Parse(typeof(MPClassType), sr.ReadLine(), true);
            int n_attr = Convert.ToInt32(sr.ReadLine());
            while (n_attr > 0)
            {
                attr.Add(sr.ReadLine());
                n_attr--;
            }
        }
        
        public void Save(StreamWriter sw)
        {
            sw.WriteLine(name);
            sw.WriteLine(id);
            sw.WriteLine(type);
            sw.WriteLine(attr.Count());
            foreach (string str in attr)
            {
                sw.WriteLine(str);
            }
        }
    }
}
